<?php
namespace app\common\controller;

use think\Model;
use think\Db;
use think\Loader;
use think\Exception;
use think\exception\HttpException;
use app\admin\model\AdminUser as AdminUserModel;
use app\common\controller\Common;

class Admin extends Common
{
    protected function _initialize()
    {

        parent::_initialize();
        defined('UID') || $this->checkAuth();
        $var_3 = 'before' . $this->request->{action}();
        if (method_exists($this, $var_3)) {
            $this->$var_3();
        }
    }

    protected function search($var_4, $var_5 = [])
    {

        $var_8 = [];
        $var_9 = $var_4->getTableInfo();
        $var_5 = array_merge($this->request->param(), $var_5);
        foreach ($var_5 as $var_10 => $var_11) {
            if ($var_11 !== '' && in_array($var_10, $var_9['fields'])) {
                $var_8[$var_10] = $var_11;
            }
        }
        return $var_8;
    }

    protected function getModel($var_12 = '', $var_13 = false)
    {

        if (!$var_12) {
            $var_12 = $this->request->{controller}();
        }
        if (class_exists($var_16 = Loader::parseClass('admin', model, $var_12))) {
            $var_17 = new $var_16();
            $var_18 = model;
        } else {
            $var_17 = Db::name($var_12);
            $var_18 = db;
        }
        return $var_13 ? ['type' => $var_18, model => $var_17] : $var_17;
    }

    protected function updateField($var_19, $var_20, $var_21 = "操作成功", $var_22 = "", $var_23 = "")
    {

        $var_26 = $this->getModel();
        if (!$var_22) {
            $var_22 = $var_26->getPk();
        }
        if (!$var_23) {
            $var_23 = $var_26->getPk();
        }
        $var_27 = $this->request->param($var_23);
        $var_28[$var_22] = ['in', $var_27];
        if (!1 === $var_26->where($var_28)->update([$var_19 => $var_20])) {
            $this->error($var_26->getError());
        }
        $this->success($var_21, 'layer');
    }

    public function quickEdit()
    {

        if ($this->request->isAjax() && $this->request->isGet()) {
            $var_31 = input('get.id', '');
            $var_32 = input('get.field', '');
            $var_33 = input('get.value', '');
            if ($var_31 == '') $this->error('缺少主键值');
            if ($var_32 == '') $this->error('缺少字段名');
            if ($var_33 == '') $this->error('缺少相应的值');
            $this->updateField($var_32, $var_33, '修改成功');
        } else {
            $this->error('请求方式不正确');
        }
    }

    protected function checkLogin()
    {

        $var_36 = $this->getModel('AdminUser');
        if ($var_37 = $var_36->isLogin()) {
            return $var_37;
        } else {
            $this->{redirect}('Login/index');
        }
    }

    protected function checkAuth()
    {

        $var_40 = new \chengxuxia\Auth();
        $var_41 = $this->request->{controller}();
        $var_42 = $this->request->{action}();
        $var_43 = $var_41 . '/' . $var_42;
        $var_44 = array('Login/index', 'Login/out');
        if (session('user_auth.gid') != 1) {
            if (!in_array($var_43, $var_44)) {
                if (!$var_40->check($var_43, session('user_auth.uid'))) {
                    $this->error('没有权限');
                }
            }
        }
    }

    protected function filterId($var_45, $var_46 = '该记录不能执行此操作', $var_47 = 'in_array', $var_48 = 'id')
    {

        $var_51 = $this->request->param();
        if (!isset($var_51[$var_48])) {
            $this->error('缺少必要参数');
        }
        $var_52 = is_array($var_51[$var_48]) ? $var_51[$var_48] : explode(',', $var_51[$var_48]);
        foreach ($var_52 as $var_53) {
            switch ($var_47) {
                case '<':
                case 'lt':
                    $var_54 = $var_53 < $var_45;
                    break;
                case '>':
                case 'gt':
                    $var_54 = $var_53 < $var_45;
                    break;
                case '=':
                case 'eq':
                    $var_54 = $var_53 == $var_45;
                    break;
                case '!=':
                case 'neq':
                    $var_54 = $var_53 != $var_45;
                    break;
                default:
                    $var_54 = call_user_func_array($var_47, [$var_53, $var_45]);
                    break;
            }
            if ($var_54) {
                $this->error($var_46);
            }
        }
    }

    protected function datalist($var_55, $var_56, $var_57 = null, $var_58 = '', $var_59 = false, $var_60 = false, $var_61 = true)
    {

        $var_64 = ['_table', '_relation', '_field', '_order_by', '_paginate', '_model'];
        if (isset($var_56['_model'])) {
            $var_55 = $var_56['_model'];
        }
        $var_65 = $this->request->param('_order') ?: (empty($var_58) ? $var_55->getPk() : $var_58);
        $var_66 = null !== $this->request->param('_sort') ? ($this->request->param('_sort') == 'asc' ? 'asc' : 'desc') : ($var_59 ? 'asc' : 'desc');
        if (isset($var_56['_relation'])) {
            $var_55 = $var_55::with($var_56['_relation']);
        }
        if (isset($var_56['_field'])) {
            $var_57 = $var_56['_field'];
        }
        if (isset($var_56['_table'])) {
            if (strpos($var_65, '.') === !1) {
                $var_65 = $var_56['_table'] . '.' . $var_65;
            }
            $var_67 = is_array($var_57) ? $var_57 : explode(',', $var_57);
            foreach ($var_67 as &$var_68) {
                if (strpos($var_68, '.') === !1) {
                    $var_68 = preg_replace('/([^\\s\\(\\)]*[a-z0-9\\*])/', $var_56['_table'] . '.$1', $var_68, 1);
                }
            }
            $var_57 = implode(',', $var_67);
            foreach ($var_56 as $var_69 => $var_68) {
                if (!in_array($var_69, $var_64) && strpos($var_69, '.') === !1) {
                    $var_56[$var_56['_table'] . '.' . $var_69] = $var_68;
                    unset($var_56[$var_69]);
                }
            }
        }
        $var_70 = $var_65 ? "{$var_65} {$var_66}" : !1;
        if (isset($var_56['_order_by'])) {
            $var_70 = $var_56['_order_by'];
        }
        $var_61 = isset($var_56['_paginate']) ? boolval($var_56['_paginate']) : $var_61;
        foreach ($var_64 as $var_68) {
            unset($var_56[$var_68]);
        }
        if ($var_61) {
            $var_71 = $this->request->param('numPerPage') ?: config('paginate.list_rows');
            $var_72 = $var_55->field($var_57)->where($var_56)->order($var_70)->paginate($var_71, !1, ['query' => $this->request->param()]);
            if ($var_60) {
                return $var_72;
            } else {
                $this->assign('list', $var_72);
                $this->assign(count, $var_72->total());
                $this->assign('page', $var_72->render());
                $this->assign('numPerPage', $var_72->listRows());
            }
        } else {
            $var_72 = $var_55->field($var_57)->where($var_56)->order($var_70)->select();
            if ($var_60) {
                return $var_72;
            } else {
                $this->assign('list', $var_72);
                $this->assign(count, count($var_72));
                $this->assign('page', '');
                $this->assign('numPerPage', 0);
            }
        }
    }

    public function ajaxUploadImg()
    {

        if ($this->request->param('position')) {
            $var_75 = $this->request->param('position');
        } else {
            return $this->ajaxReturn('', '未设置图片上传位置', 0);
        }
        $var_76 = $this->request->param('type') ? $this->request->param('type') : 'image';
        $var_77 = $this->request->{file}($var_76);
        $var_78 = ROOT_PATH . 'data/uploads/' . $var_75 . '/';
        $var_79 = $var_77->{validate}(['size' => 2048 * 1204, 'ext' => 'jpg,png,gif'])->move($var_78);
        if ($var_79) {
            $var_80 = '/data/uploads/' . $var_75 . '/' . str_replace('\\', '/', $var_79->getSaveName());
            return $this->ajaxReturn($var_80, '图片上传成功');
        } else {
            return $this->ajaxReturn('', $var_77->getError(), 0);
        }
    }
}
